package com.example.myapplication.views;

import static android.content.ContentValues.TAG;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ViewAnimator;

import androidx.annotation.RequiresApi;

import com.example.myapplication.R;

import java.time.LocalDateTime;

public class RefreshList extends ListView {
    private View headerView;
    //private View footerView;
    private int height;
    private TextView tvActionTip;
    private ImageView ivArrow;
    private ProgressBar pbRefreshing;
    TextView footerTV;

    private final static int STATE_DONE = 1;
    private final static int STATE_PULL = 2;
    private final static int STATE_RELEASE = 3;
    private final static int STATE_REFRESHING = 4;
    private int currentState;
    private int downY;

    private long timeEllipse = 2;
    private LocalDateTime last;

    // 2.申明接口
    private OnRefreshListener onRefreshListener;
    @RequiresApi(api = Build.VERSION_CODES.O)
    public RefreshList(Context context, AttributeSet attrs) {
        super(context, attrs);
     //   footerView = View.inflate(context,R.layout.list_footer,null);
        headerView = View.inflate(context, R.layout.refresh_header, null);
        this.addHeaderView(headerView);
       // this.addFooterView(footerView);

        headerView.measure(0, 0);
        height = headerView.getMeasuredHeight();
        headerView.setPadding(0, -height, 0, 0);
        initView();
        last = LocalDateTime.now();
        currentState = STATE_DONE;
    }

    private void initView() {
      //  this.footerTV = footerView.findViewById(R.id.info);
        tvActionTip = (TextView) headerView.findViewById(R.id.tips);
        ivArrow = (ImageView) headerView.findViewById(R.id.arrow);
        pbRefreshing = (ProgressBar) headerView.findViewById(R.id.progress);

    }
    boolean isRefresh = false;

    @Override
    @RequiresApi(api = Build.VERSION_CODES.O)
    public boolean onTouchEvent(MotionEvent ev) {
        try {
            int action = ev.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:// 往下滑的手势
                    if (currentState == STATE_DONE) {// 只有在完成状态时才会有业务动作
// 触发下拉手势的Y坐标
                        downY = (int) ev.getY();
                        Log.d(TAG, "onTouchEvent: "+downY);
                        //if(downY < height+10){
                            currentState = STATE_PULL;
                        //}
// 切换状态
                    }
                    break;
                case MotionEvent.ACTION_MOVE:// 正在拖动的手势
                    if (currentState == STATE_PULL) {// 只有在下拉状态时才会有动作
                        int currentY = (int) ev.getY();// 得到正在不断改变的当前Y坐标
                        int top = currentY - downY - height;// 这个值是下拉时头部视图的高度显示，要慢慢变化
                        headerView.setPadding(0, top, 0, 0);
                        if (currentY - downY > height){// 如果下拉的高度超过了头视图的高度，则改变状态
                            currentState = STATE_RELEASE;
                            tvActionTip.setText("松开刷新");
                        }
                        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(ivArrow, "rotation", 360f,0);
                        objectAnimator.setDuration(500).start();
                    } break;
                case MotionEvent.ACTION_UP:
                    if (currentState == STATE_RELEASE) {// 只有在释放状态时才进行刷新动作
                        tvActionTip.setText("刷新中");// 改变文字 提示
                        currentState = STATE_REFRESHING;// 改变状态，变为刷新状态
                        ivArrow.setVisibility(GONE);// 箭头隐藏
                        pbRefreshing.setVisibility(VISIBLE);// 进度条显示
                        isRefresh = true;
                        if (onRefreshListener != null) {
                            onRefreshListener.OnRefresh(this);
                            refreshComplete();
                        }
                    } else {
                        if (!isRefresh) {
                            headerView.setPadding(0, -height, 0, 0);
                            currentState = STATE_DONE;
                        }
                    }
                    break;
            }
        } catch (Exception e) {
        } return super.onTouchEvent(ev);
    }

    // 3.传递接口
    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }


    public void refreshComplete() {
        ValueAnimator animator = ValueAnimator.ofFloat(0, -height);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float f = (float)valueAnimator.getAnimatedValue();
                headerView.setPadding(0, (int) f, 0, 0);
            }
        });
        animator.start();
        isRefresh = false;
//更新数据结果后，再回调这个方法
// 一定要记得把状态改回去，不然会没法再次向下拉
        currentState = STATE_DONE;
//将进度条设为不可见，将箭头设为可见，将文字改回去
        ivArrow.setVisibility(VISIBLE);// 箭头显示
        pbRefreshing.setVisibility(GONE);// 进度条隐去
        tvActionTip.setText("下拉刷新");// 改变文字 提示
    }


}